import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Layout from '@/views/Layout'
import Article from '@/views/Article'
import Dashboard from '@/views/Dashboard'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      redirect: '/dashboard',
      component: Layout,
      children: [
        { path: '/dashboard', component: Dashboard },
        { path: '/article', component: Article }
      ]
    }
  ]

})
// 前置路由导航守卫
// 登录拦截
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  // 如果没有登录还要去非登录页面，只能让你去登录
  if (!token && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }
})

export default router
